package com.lczyfz.wisdom.common.controlleradvice;

import com.lczyfz.wisdom.common.utils.ServerResponse;
import org.apache.shiro.authz.UnauthenticatedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class ApiControllerAdvice {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @ResponseBody
    @ExceptionHandler(UnauthenticatedException.class)
    public ServerResponse<String> unauthenticatedException(HttpServletRequest request, HttpServletResponse response, Exception e) {

        String url = "";
        url = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()
                + request.getServletPath();
        if (request.getQueryString() != null) {
            url += "?" + request.getQueryString();
        }
        logger.debug("e", e);
        logger.debug("请先登录");
        logger.debug(url);
        response.setStatus(401);
        return ServerResponse.unLogin() ;  }
}

